<?php

// 初期設定
if (!defined("IN_APP")) {
	define("IN_APP", true);
	include_once("../../include/init.php");
	requireLogin();
}

// タブ別の設定の読み込み
require_once("tab.include.php");

// ページ名
$page_name = "attendance";

/******************* ページの処理 *******************/

include_once($SETTINGS['INCLUDE_PATH'] . "/attendance_check.php");
include_once($SETTINGS['INCLUDE_PATH'] . "/Twitter/get_icons.php");

class Member {
	var $icon, $name, $twitter_name, $id, $status, $buttonToShow;
}

$attendance_check = attendanceCheck(time());

$connect = connectToDb();
$hide_status = false;

$group_id = $_SESSION['group'];
$group_name = getGroupName($connect, $group_id);

// 出欠確認がされていない状態
if ($attendance_check == 0) {
	$attendance_start_enabled = true;
	$hide_status = true;
// ツイッターのアクセスに失敗した
} else if ($attendance_check == -1) {
	$error = "ツイッターのリストからツイートを取得できませんでした。";
	showErrorPage();
// 出欠確認がされている状態
} else {
	$attendance_start_enabled = false;
}

// リーダーかどうかを判断する
$isLeader = isLeader($connect, $group_id, $_SESSION['user_id']);

$groups = array();

$query = "SELECT users.name,users.twitter_username,users.user_id FROM group_members AS member ";
$query .= "INNER JOIN users ON member.user_id = users.user_id ";
$query .= "WHERE group_id = " . mysql_real_escape_string($group_id) . " ";
$query .= "AND (member.del_flag <> 'd' OR member.del_flag IS NULL) ";
$query .= "ORDER BY member.sort_order ASC";

$result = mysql_query($query, $connect);

$attendance = array();



if ( !$result ) {
	exit("Invalid query: $query - " . mysql_error());
}
else {
	$id_list = array();
	$member_list = array();
	
	while ($row = mysql_fetch_assoc($result)) {
		array_push($id_list, $row['user_id']);
		
		$member_list[$row['user_id']] = $row["twitter_username"];
	}
	
	$icons = getIcons($id_list);
	
	foreach ($member_list as $id => $twitter_username) {
		if (!$hide_status) {
			// データベースからユーザーの情報を取得する
			$query = "SELECT status FROM attendance ";
			$query.= "WHERE group_id = " . mysql_real_escape_string($_SESSION['group']) . " ";
			$query.= "AND user_id = " . mysql_real_escape_string($id) . " ";
			$query.= "AND date = '" . date("Y-m-d") . "' ";
			$query.= "AND (del_flag <> 'd' OR del_flag IS NULL) ";
			$query.= "ORDER BY time DESC ";
			$query.= "LIMIT 0,1";
							
			$res = mysql_query($query, $connect);
			
			$attend_row = mysql_fetch_assoc($res);
			
			if (!empty($attend_row)) {
				if ($attend_row['status'] == 1) {
					$status = "出席";
				}
				else if ($attend_row['status'] == 0) {
					$status = "欠席";
				}
			}
			else {
				$status = "未回答";
			}
		}
		else {
			$status = "";
		}
		
		$username = getUserName($connect, $_SESSION['group'], $id);
		
		// メンバー情報をオブジェクトに入れる
		$member = new Member();
		$member->name = $username;
		$member->id = $id;
		$member->twitter_name = $twitter_username;
		$member->status = $status;
		$member->icon = $icons[$id];
		
		switch ($status) {
			case '出席':
				$member->buttonToShow = "cross";
				break;
			case '欠席':
				$member->buttonToShow = "circle";
				break;
			default:
				$member->buttonToShow = "circle";
				break;
		}
		
		array_push($attendance, $member);
	}
}

mysql_close($connect);

/******************* 処理終了 *******************/


// ページを表示する
$include_path = $SETTINGS['VIEW_PATH'] . $tab_name . "/" . $page_name . $SETTINGS['VIEW_NAME'];
include($include_path);

?>